package com.sadam.ui4.FragmentCommentPage;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.signature.ObjectKey;
import com.sadam.ui4.Data.Comment;
import com.sadam.ui4.Data.MySqLiteOpenHelper;
import com.sadam.ui4.Data.User;
import com.sadam.ui4.R;

public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.CommentViewHolder> {
    private List<Comment> comments = new ArrayList<>();

    static class CommentViewHolder extends RecyclerView.ViewHolder {
        ImageView imageViewAvatar;
        TextView textViewUsername, textViewContent, textViewTime;

        public CommentViewHolder(@NonNull View itemView) {
            super(itemView);
            imageViewAvatar = itemView.findViewById(R.id.imageView_avatar);
            textViewUsername = itemView.findViewById(R.id.textView_username);
            textViewContent = itemView.findViewById(R.id.textView_content);
            textViewTime = itemView.findViewById(R.id.textView_time);
        }
    }

    @NonNull
    @Override
    public CommentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_comment, parent, false);
        return new CommentViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CommentViewHolder holder, int position) {
        if (comments == null || position >= comments.size()) {
            Log.e("CommentAdapter", "Invalid comment list or position");
            return;
        }

        Comment comment = comments.get(position);
        if (comment == null) {
            Log.e("CommentAdapter", "Comment is null at position: " + position);
            return;
        }

        try {
            holder.textViewUsername.setText(comment.getUserName() != null ? comment.getUserName() : "未知用户");
            holder.textViewContent.setText(comment.getContent() != null ? comment.getContent() : "");
            holder.textViewTime.setText(formatTime(comment.getTime()));

            // 加载头像
            String avatarUrl = comment.getUserAvatarUrl();
            if (avatarUrl != null && !avatarUrl.isEmpty()) {
                Glide.with(holder.itemView.getContext())
                        .load(avatarUrl)
                        .signature(new ObjectKey(System.currentTimeMillis() / (1 * 1000)))
                        .placeholder(R.drawable.shape_round)
                        .error(R.drawable.shape_round)
                        .apply(RequestOptions.circleCropTransform())
                        .into(holder.imageViewAvatar);
            } else {
                holder.imageViewAvatar.setImageResource(R.drawable.shape_round);
            }
        } catch (Exception e) {
            Log.e("CommentAdapter", "Error binding comment data", e);
        }
    }

    @Override
    public int getItemCount() {
        return comments.size();
    }

    public void setComments(List<Comment> comments) {
        this.comments = comments;
        notifyDataSetChanged();
    }

    private String formatTime(long timestamp) {
        long diff = System.currentTimeMillis() - timestamp;
        if (diff < 60_000) return "刚刚";
        else if (diff < 3600_000) return (diff / 60_000) + "分钟前";
        else if (diff < 86400_000) return (diff / 3600_000) + "小时前";
        else return (diff / 86400_000) + "天前";
    }
}